package jok.thread;

public class TestTicket {

	public static void main(String[] args) {

		// 多线程未加同步锁
		// testTicketThread();

		// 多线程加同步锁：使用synchronized代码块，锁对象为this，多线程调用相同对象
		// testSyncTicketThreadWithThisBlock();

		// 多线程加同步锁：使用synchronized代码块，锁对象为this，多线程调用不同对象
		// testSyncTicketThreadWithThisBlock2();

		// 多线程加同步锁：使用synchronized修饰对象方法，多线程调用相同对象
		// testSyncTicketThreadWithThisMethod();

		// 多线程加同步锁：使用synchronized修饰对象方法，多线程调用不同对象
		// testSyncTicketThreadWithThisMethod2();

		// 多线程加同步锁：使用synchronized代码块，锁对象为当前类，多线程调用不同对象
		// testSyncTicketThreadWithClassBlock();

		// 多线程加同步锁：使用synchronized修饰静态方法（类方法），多线程调用不同对象
		testSyncTicketThreadWithStaticMethod();
	}

	/**
	 * 多线程未加同步锁
	 */
	private static void testTicketThread() {
		TicketThread tt = new TicketThread();

		for (int i = 1; i <= 4; i++) {
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized代码块，锁对象为this，多线程调用相同对象
	 */
	private static void testSyncTicketThreadWithThisBlock() {
		SyncTicketThreadWithThisBlock tt = new SyncTicketThreadWithThisBlock();

		for (int i = 1; i <= 4; i++) {
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized代码块，锁对象为this，多线程调用不同对象
	 */
	private static void testSyncTicketThreadWithThisBlock2() {
		for (int i = 1; i <= 4; i++) {
			SyncTicketThreadWithThisBlock tt = new SyncTicketThreadWithThisBlock();
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized修饰对象方法，多线程调用相同对象
	 */
	private static void testSyncTicketThreadWithThisMethod() {
		SyncTicketThreadWithThisMethod tt = new SyncTicketThreadWithThisMethod();

		for (int i = 1; i <= 4; i++) {
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized修饰对象方法，多线程调用不同对象
	 */
	private static void testSyncTicketThreadWithThisMethod2() {
		for (int i = 1; i <= 4; i++) {
			SyncTicketThreadWithThisMethod tt = new SyncTicketThreadWithThisMethod();
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized代码块，锁对象为当前类，多线程调用不同对象
	 */
	private static void testSyncTicketThreadWithClassBlock() {
		for (int i = 1; i <= 4; i++) {
			SyncTicketThreadWithClassBlock tt = new SyncTicketThreadWithClassBlock();
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}

	/**
	 * 多线程加同步锁：使用synchronized修饰静态方法（类方法），多线程调用不同对象
	 */
	private static void testSyncTicketThreadWithStaticMethod() {
		for (int i = 1; i <= 4; i++) {
			SyncTicketThreadWithStaticMethod tt = new SyncTicketThreadWithStaticMethod();
			Thread t = new Thread(tt, "窗口" + i);
			t.start();
		}
	}
}
